package read;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class readXml{
	
	static String invoiceComplete = new String();
	static String invoices[], details[];
	
	public static void leer(){
		String ruta="/home/yohan/Escritorio/mycoolcell08.xml";
		try {
			 File f = new File( ruta );
			 BufferedReader entrada = new BufferedReader( new FileReader( f ) );
			 String linea;
	         while((linea = entrada.readLine()) != null)
	            invoiceComplete+=linea;
	         invoiceComplete = deleteSubString(invoiceComplete, "<root>");
	         invoiceComplete = deleteSubString(invoiceComplete, "</root>");
	         invoiceComplete = deleteSubString(invoiceComplete, "</InvoiceItem>");
	         invoices = invoiceComplete.split("<InvoiceItem>");
	         for(int k = 1; k<invoices.length; k++){
	        	 System.out.println("--------- New invoice " + k + " -------------");
	        	 System.out.println(separateTag(invoices[k], "<PO_No>"));
		         invoices[k] = deleteTag(invoices[k],"<PO_No>");
		         System.out.println(separateTag(invoices[k], "<Invoice_No>"));
		         invoices[k] = deleteTag(invoices[k],"<Invoice_No>");
		         System.out.println(separateTag(invoices[k], "<Date>"));
		         invoices[k] = deleteTag(invoices[k],"<Date>");
		         System.out.println(separateTag(invoices[k], "<Sold_To>"));
		         invoices[k] = deleteTag(invoices[k],"<Sold_To>");
		         System.out.println(separateTag(invoices[k], "<Ship_To>"));
		         invoices[k] = deleteTag(invoices[k],"<Ship_To>");
		         System.out.println(separateTag(invoices[k], "<Ship_Via>"));
		         invoices[k] = deleteTag(invoices[k],"<Ship_Via>");
		         System.out.println(separateTag(invoices[k], "<Total_Qty>"));
		         invoices[k] = deleteTag(invoices[k],"<Total_Qty>");
		         System.out.println(separateTag(invoices[k], "<Sale_Amount>"));
		         invoices[k] = deleteTag(invoices[k],"<Sale_Amount>");
		         System.out.println(separateTag(invoices[k], "<Shipping_Handling>"));
		         invoices[k] = deleteTag(invoices[k],"<Shipping_Handling>");
		         System.out.println(separateTag(invoices[k], "<Total_Amount>"));
		         invoices[k] = deleteTag(invoices[k],"<Total_Amount>");
		         invoices[k] = deleteSubString(invoices[k], "<OrderItems>");
		         invoices[k] = deleteSubString(invoices[k], "</OrderItems>");
		         invoices[k] = deleteSubString(invoices[k],"</OrderItem>");
		         details = invoices[k].split("<OrderItem>");
		         for(int i=1;i<details.length;i++){
		        	 System.out.println("Producto "+(i+1));
		        	 System.out.println("Cantidad Ordernada: "+separateTag(details[i], "<Order_Qty>"));
		        	 System.out.println("Cantidad Enviada: "+separateTag(details[i], "<Ship_Qty>"));
		        	 System.out.println("Numero Producto: "+separateTag(details[i], "<Item_No>"));
		        	 System.out.println("Descripción: "+separateTag(details[i], "<Description>"));
		        	 System.out.println("Precio Unitario: "+separateTag(details[i], "<Unit_Price>"));
		        	 System.out.println("Precio Extendido: "+separateTag(details[i], "<Ext_Amount>"));
		         }
	         }
		} catch (IOException e) {
			e.printStackTrace();
		}  
	    
	}
	
	public static String separateTag(String cadena, String tag){
		String out = new String();
		String endTag = "</" + tag.substring(1,tag.length());
		int initPosTag = cadena.indexOf(tag) + tag.length();
		int endPosTag = cadena.indexOf(endTag);
		out = cadena.substring(initPosTag,endPosTag);
		return out;
	}
	
	public static String changeSubString(String cadena, String replace, String newValor){
		String out = new String();
		int pos = cadena.indexOf(replace);
		if( pos != -1){
			out = cadena.substring(0, pos);
			out += newValor;
			out += cadena.substring( (pos+replace.length()),cadena.length() );
		}
		return out;
	}
	
	public static String deleteTag(String cadena, String tag){
		String out = new String();
		String endTag = "</" + tag.substring(1,tag.length());
		int initpos = cadena.indexOf(tag);
		int endpos = cadena.indexOf(endTag) + endTag.length();
		if( initpos != -1 && endpos !=1){
			out = cadena.substring(0, initpos);
			out += cadena.substring( endpos ,cadena.length() );
		}
		return out;
	}
	
	public static String deleteSubString(String cadena, String replace){
		String out = new String();
		int pos = cadena.indexOf(replace);
		if( pos != -1){
			out = cadena.substring(0, pos);
			out += cadena.substring( (pos+replace.length()),cadena.length() );
		}
		return out;
	}
	
	 public static void main(String [] arg) {
		 leer();
	 }

}
